from devicetest.api import Asserts

from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step
from devicetest.log.logger import print_info

from aw.common import common_setup, common_teardown
from aw.pom import DeskTop, Keyboard


class PreTest_RK3568_Basic_Desktop_0700(TestCase, DeskTop):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1、进入桌面，长按应用拖动合并到文件夹中
    # 2、长按文件夹重命名
    # 3、进入文件夹中添加应用、去除应用
    # 预期结果：
    # 1、应用合并到文件夹中无异常
    # 2、文件重命名显示正常
    # 3、文件夹内可正常添加应用，应用移除正常

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1、进入桌面，长按应用拖动合并到文件夹中')
        CommonOH.drag(self.Phone1, sx=80, sy=320, ex=220, ey=320)
        Step('预期结果1、应用合并到文件夹中无异常')
        CommonOH.checkIfTextExist(self.Phone1, '新建文件夹1')

        Step('步骤2、长按文件夹重命名')
        x1, y1 = CommonOH.getPointByCondition(self.Phone1, '新建文件夹1')
        CommonOH.click(self.Phone1, x=x1, y=y1 - 110, mode='LONG')
        CommonOH.touchByText(self.Phone1, '重命名')
        CommonOH.click(self.Phone1, x=547, y=209)
        CommonOH.touchByType(self.Phone1, 'TextInput')
        CommonOH.click(self.Phone1, **Keyboard.key_a)
        CommonOH.click(self.Phone1, **Keyboard.close)
        # 第一次退出编辑，第二次退出文件夹
        CommonOH.click(self.Phone1, **self.center)
        CommonOH.click(self.Phone1, **self.center)
        Step('预期结果2、文件重命名显示正常')
        CommonOH.checkIfTextExist(self.Phone1, 'a')

        Step('步骤3、进入文件夹中添加应用、去除应用')
        CommonOH.click(self.Phone1, x=x1, y=y1 - 110)
        CommonOH.wait(self.Phone1, 0.5)
        x2, y2 = CommonOH.getPointByCondition(self.Phone1, '添加')
        CommonOH.click(self.Phone1, x=x2, y=y2 - 55)
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.touchByType(self.Phone1, 'Toggle', index=2)
        CommonOH.touchByText(self.Phone1, '确认')
        Step('预期结果3、文件夹内可正常添加应用，应用移除正常')
        x3, y3 = CommonOH.getPointByCondition(self.Phone1, '添加')
        # "添加"的位置变了说明应用添加成功
        Asserts.assert_not_equal(x3, x2)
        Asserts.assert_not_equal(y2, y3)
        CommonOH.click(self.Phone1, x=x3, y=y3 - 55)
        CommonOH.touchByType(self.Phone1, 'Toggle', index=0)
        CommonOH.touchByType(self.Phone1, 'Toggle', index=1)
        CommonOH.touchByType(self.Phone1, 'Toggle', index=2)
        CommonOH.touchByText(self.Phone1, '确认')
        CommonOH.checkIfTextExist(self.Phone1, 'a', expect=False)

    def teardown(self):
        common_teardown(self.Phone1)
